Lab 13
Function Types and Scope
CS211 Lab Policy:
- This lab exercise will not be graded.
- Submit as much as you have completed before the end of the lab period in
which it is assigned.
- If you do not finish this lab work, it is to your advantage to finish it
outside of class. Please re-submit your finished work to the course web
site.
- You may receive help from anyone in completing this lab.
- You may not submit another student's code as part of your
lab.
Instructions:
This lab is divided into 2 parts. You only will submit your file for part 2.
Part 1: Investigate function scope
Copy and paste the MATLAB code below and save it into a file called sam.m.
function sam()
function mary()
fprintf('nested function mary was called\n');
end % nested
function mary
function joe()
fprintf('nested function joe was called\n');
mary();
end % nested
function joe
clc();
fprintf('primary function sam was called\n');
mary();
joe();
end % primary
function sam
function mary()
fprintf('subfunction mary was called\n');
end % subfunction
mary
function fred()
fprintf('subfunction fred was called\n');
mary();
end % subfunction
fred |
Perform the following tasks and modifications on this file and examine the
results to help you better understand function scope. If there is anything you
do not understand, please discuss it with your instructor.
- Execute this file.
(Type sam() at the command
line.)
- Do you understand which functions were called and why?
- Refer to the 'MATLAB's search order to find functions'
section in the lesson notes.
- Add a call to the function 'fred' from the body of 'sam'.
- Did it work?
- Which version of 'mary' did subfunction 'fred' call? Why?
- Add a call to 'sam' from subfunction 'fred'.
- What happened?
- What does this tell you about the scope of 'sam'?
- Add a call to 'fred' from subfunction 'mary'.
Part 2: Investigate how functions
facilitate the organization of complex code
Save the file Ticktacktoe.m to your CS211 programs
folder. Then execute the function to play "Tick Tack Toe".Now
modify the code, as described below, to separate the single function Ticktacktoe
into logical sub-tasks (i.e., separate subfunctions). Hopefully, when you
are finished with these modifications, you will be able to compare the original
version with your new modularized version and come to the conclusion that your
modified version is better structured, more "readable", and would be easier to
debug and maintain over time.
- Highlight lines 21-55, cut them, and then paste them at the bottom of
your file.
Above these copied lines, add a function header and name this subfunction
Initialize_Board.
Give this function one dummy input argument,
Board_Size.
Give this function one dummy output argument, Board.
Add an end statement at the end the
copied lines (at end of the file).
Add a comment separator line above the function header. (i.e.,
%-----------------------------------------------------)
Add a call to this new subfunction on line 21 of your Ticktacktoe
function.
Execute your code to make sure it still runs correctly.
- Copy and paste the lines of code that get the user input for the next
move into a new subfunction.
Does this code need any data from the main Ticktacktoe function to accomplish
its task? If so, create appropriate input arguments.
Does this code need to return any values to the main Ticktacktoe function? If
so, create appropriate output arguments.
Add a call to this subfunction in the same place where you removed
the lines of code.
Execute your code to make sure it still runs correctly.
- There are several other blocks of code that should probably be
subfunctions.
Decide which blocks of code you think should be separated into separate
subfunctions and "make it so".
- Hopefully you now have a program that does exactly what the original did -- it
plays ticktacktoe!
BUT, this version has better structure that will facilitate
bug fixes and enhancements to the program over the coming years.
Note: This "Tick Tack Toe" program is not robust -- it does not check for
errors! If you were given the task of making it robust, your amount of code
would increase greatly. This is an additional reason why modularizing your code
is beneficial for large programs. (Just for fun -- not required, try to make
your ticktacktoe program robust!)Turn-in:
Submit your
new, modified ticktacktoe.m file.